#include "stdafx.h"
#include "MyDef.h"
#include "CMain.h"
#include "CDevice.h"
#include "CTexture.h"
#include "CPoly.h"

CPoly::CPoly()
{
	if( FAILED(DEVICE->CreateVertexBuffer( sizeof(VERTEX)*4, D3DUSAGE_WRITEONLY, (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1), D3DPOOL_MANAGED, &m_Buffer, NULL)))
	{
		throw ::MessageBoxA(NULL, "","",MB_OK);
	}
}	

CPoly::~CPoly()
{
	SAFE_RELEASE(m_Buffer);
}

void CPoly::Init(float _Width, float _Height)
{
	m_Vertex[0].m_Pos = D3DXVECTOR4( -_Width/2, -_Height/2, 0, 1.0f );
	m_Vertex[1].m_Pos = D3DXVECTOR4(  _Width/2, -_Height/2, 0, 1.0f );
	m_Vertex[2].m_Pos = D3DXVECTOR4(  _Width/2,  _Height/2, 0, 1.0f );
	m_Vertex[3].m_Pos = D3DXVECTOR4( -_Width/2,  _Height/2, 0, 1.0f );

	m_Vertex[0].m_Tex = D3DXVECTOR2(0, 0);
	m_Vertex[1].m_Tex = D3DXVECTOR2(1, 0);
	m_Vertex[2].m_Tex = D3DXVECTOR2(1, 1);
	m_Vertex[3].m_Tex = D3DXVECTOR2(0, 1);

	for(int i=0; i<4; i++)
	{
		m_Vertex[i].m_Col = 0xffffffff;
	}

	memcpy( m_Cp, m_Vertex, sizeof(VERTEX)*4 );
}

void CPoly::Draw(LPDIRECT3DTEXTURE9 _pTex)
{
	void *pData;
	if( SUCCEEDED( m_Buffer->Lock( 0, sizeof(VERTEX)*4, (void**)&pData, 0)))
	{
		memcpy(pData, m_Cp, sizeof(VERTEX)*4 );
		m_Buffer->Unlock();
	}

	D3DXMATRIX mat;
	D3DXMatrixTranslation(&mat, 100,100,0);

	DEVICE->SetTransform(D3DTS_WORLD, &mat);
	DEVICE->SetStreamSource(0, m_Buffer, 0, sizeof(VERTEX));
	DEVICE->SetTexture(0, _pTex);
	DEVICE->SetFVF( D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
	DEVICE->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, pData, sizeof(VERTEX));
}